<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>打开网页随机显示图片</title>
</head>

<body>
    <img src="" alt="">
</body>

<script>
    // 声明图片数组 也可以直接使用图片链接，关键在于随机图片链接的序号数值
    const imgArr = [
        '../images/1.webp',
        '../images/2.webp',
        '../images/3.webp',
        '../images/4.webp',
    ];
    // 获取标签元素
    const img = document.querySelector('img');

    // 设置img图片src链接
    img.src = `../images/${getRandom(1, imgArr.length)}.webp`;

    // 封装随机函数
    function getRandom(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    }
</script>

</html>